package Song.Han.demo04;

import java.util.HashMap;
import java.util.Set;

/*
* HashMap存储自定义类型的键与值
* Map集合key时唯一的，所有key的类型必须重写hashcode方法和equals方法*/
public class Demo01HashMapSavePerson {

    public static void main(String[] args) {
        show02();
    }

    /*
     * key:Person类型    *自定义类必须重写hashCode和equals方法，以保证key唯一
     * value:String类型
     * */
    private static void show02() {
        HashMap<Person,String> map = new HashMap<>();
        map.put(new Person("张三",18),"北京");
        map.put(new Person("李四",19),"上海");
        map.put(new Person("王五",20),"河南");
        map.put(new Person("张三",18),"深圳");

        Set<Person> set = map.keySet();
        for (Person key : set) {
            String value = map.get(key);
            System.out.println(key+"-->"+value);
        }
    }

    /*
    * key:String类型
    * value:Person类型
    * */
    private static void show01() {
        HashMap<String,Person> map = new HashMap<>();
        map.put("北京",new Person("张三",18));
        map.put("上海",new Person("李四",19));
        map.put("河南",new Person("王五",20));
        map.put("北京",new Person("赵六",18));

        //遍历
        Set<String> set = map.keySet();
        for (String key : set) {
            Person value = map.get(key);
            System.out.println(key+"-->"+value);
        }
    }
}
